package com.pocket.insight.base.sql.rewriter;

import com.pocket.insight.base.mql.Relation;
import com.pocket.insight.base.mql.relation.JoinRelation;
import com.pocket.insight.base.mql.relation.PrimeRelation;
import com.pocket.insight.base.mql.relation.StatementRelation;
import com.pocket.insight.base.mql.relation.UnionRelation;
import com.pocket.insight.base.sql.Rewriter;
import lombok.extern.slf4j.Slf4j;

import java.util.LinkedHashMap;
import java.util.concurrent.atomic.AtomicReference;

/**
 * 描述：
 *
 * @author shuize
 * @create 2024/12/10
 */
@Slf4j
public class JoinOrderRewriter implements Rewriter {

    @Override
    public Relation rewrite(Relation origin) {
        log.debug("JoinOrderRewriter start");
        if (origin instanceof StatementRelation) {
            ((StatementRelation) origin).scan(rewrite(((StatementRelation) origin).from));
        } else {
            if (origin instanceof UnionRelation) {
                UnionRelation resultRelation = new UnionRelation(origin.getDialect());
                ((UnionRelation) origin).subRelations.forEach(relation -> {
                    if (relation != null) {
                        Relation tmpRelation = rewrite(relation);
                        resultRelation.add(tmpRelation);
                    }
                });
                resultRelation.alias = ((UnionRelation) origin).alias;
                return resultRelation;
            }
            if (origin instanceof JoinRelation) {
                return (Relation) rewriteImpl((JoinRelation) origin);
            }
//            if (origin instanceof PrimeRelation) {
//                HE dataset = ((PrimeRelation) origin).dataset;
//                if (dataset.op instanceof Relation)
//                    dataset.op = rewrite((Relation) dataset.op);
//            }
        }
        return origin;
    }


    public JoinRelation rewriteImpl(JoinRelation joinRelation) {
//        Map<String, RelationNode> relationNodes = new LinkedHashMap<>();
//        String maxRelationSize = collectRelationNodes(joinRelation, relationNodes, null);
//        if (maxRelationSize == null || relationNodes.isEmpty() || maxRelationSize.equals(relationNodes.keySet().toArray()[0])) {
//            return joinRelation;
//        }
//        Map<String, RelationNode> orderedRelationNodes = new LinkedHashMap<>();
//        reOrgRelationNodes(orderedRelationNodes, relationNodes, maxRelationSize, null);
//        if (orderedRelationNodes.isEmpty()) {
//            return joinRelation;
//        }
//        AtomicReference<Relation> joinLeft = new AtomicReference<>();
//        orderedRelationNodes.forEach((alias, node) -> {
//            if (joinLeft.get() == null) {
//                joinLeft.set(node.relation);
//            } else {
//                joinLeft.set(JoinRelation.create(node.relation.getDialect(), node.joinType, joinLeft.get(), node.relation, node.conditions));
//            }
//        });
//        JoinRelation result = (JoinRelation)joinLeft.get();
//        result.orderBy = joinRelation.orderBy;
        return joinRelation;
    }

}
